package org.red.checkSystem.service.impl.check;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.red.checkSystem.pojo.Address;
import org.red.checkSystem.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.red.checkSystem.service.check.checkShowService;
import org.red.checkSystem.mapper.articleMapper;
import org.red.checkSystem.mapper.addressMapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class checkShowServiceImpl implements checkShowService{
    @Autowired
    articleMapper articleMapper;
    @Autowired
    addressMapper addressMapper;

    class withAdd{
        public int id;
        public String title;
        public String face;
        public String content;
        public String author;
        public Date time;
        public String tag;
        public Date checktime;
        public String admin;
        public String state;
        public String address;
        void  setArticle(Article article){
            this.id=article.getId();
            this.title = article.getTitle();
            this.face = article.getFace();
            this.content = article.getContent();
            this.author = article.getAuthor();
            this.time = article.getTime();
            this.tag = article.getTag();
            this.checktime = article.getChecktime();
            this.admin = article.getAdmin();
            this.state =article.getState();
        }
        void setAddress(Address address){
            this.address = address.getAddress();
        }
    };

    @Override
    public String getArticle(int id) {
        Article article = articleMapper.selectById(id);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",id);
        Address address = addressMapper.selectOne(queryWrapper);
        withAdd withAdd = new withAdd();
        withAdd.setArticle(article);
        if(address != null)
          withAdd.setAddress(address);
        return JSON.toJSONString(withAdd);
    }

    @Override
    public JSONObject showAllArticleByState(String state,int page,int sizes) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if(!state.trim().isEmpty()){
            queryWrapper.eq("state",state).orderByDesc("time");
        }else queryWrapper.orderByDesc("time");
        IPage<Article> iPage = new Page<>(page,sizes);
        List<Article> list = articleMapper.selectPage(iPage,queryWrapper).getRecords();
        System.out.println(list);
        List<withAdd> withAddList = new ArrayList<>();
        for(Article t : list){
            System.out.println(t);
            if(t.getTag().equals("红色景点")){
                System.out.println("yes");
                QueryWrapper<Address> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("aid",t.getId());
                Address address = addressMapper.selectOne(queryWrapper1);
                System.out.println(address);
                withAdd withAdd = new withAdd();
                withAdd.setArticle(t);
                withAdd.setAddress(address);
                withAddList.add(withAdd);
                System.out.println(withAdd);
            }else {
                withAdd withAdd = new withAdd();
                withAdd.setArticle(t);
                withAddList.add(withAdd);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("article",withAddList);
        jsonObject.put("length",articleMapper.selectCount(queryWrapper));
        return jsonObject;
    }

    @Override
    public JSONObject showMyUncheck(String author,int page,int sizes) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if(!author.isEmpty()){
            queryWrapper.eq("author",author).orderByDesc("time").in("state","不通过","待审核");
        }else queryWrapper.orderByDesc("time").in("state","不通过","待审核");
        IPage<Article> iPage = new Page<>(page,sizes);
        List<Article> list = articleMapper.selectPage(iPage,queryWrapper).getRecords();
        List<withAdd> withAddList = new ArrayList<>();
        for(Article t : list){
            System.out.println(t);
            if(t.getTag().equals("红色景点")){
                QueryWrapper<Address> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("aid",t.getId());
                Address address = addressMapper.selectOne(queryWrapper1);
                withAdd withAdd = new withAdd();
                withAdd.setArticle(t);
                withAdd.setAddress(address);
                withAddList.add(withAdd);
            }else {
                withAdd withAdd = new withAdd();
                withAdd.setArticle(t);
                withAddList.add(withAdd);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("article",withAddList);
        jsonObject.put("length",articleMapper.selectCount(queryWrapper));
        return jsonObject;
    }

    @Override
    public JSONObject ShowAll(int page, int sizes) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        IPage<Article> iPage = new Page<>(page,sizes);
        List<Article> list = articleMapper.selectPage(iPage,queryWrapper).getRecords();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("article",list);
        jsonObject.put("length",articleMapper.selectCount(queryWrapper));
        return jsonObject;
    }
}
